package com.ynet.middleground.contract.service.impl;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.contract.bean.DeleteArchiveReq;
import com.ynet.middleground.contract.bean.EditArchiveReq;
import com.ynet.middleground.contract.bean.NewArchiveReq;
import com.ynet.middleground.contract.bean.QueryArchiveReq;
import com.ynet.middleground.contract.dto.ArchiveDto;
import com.ynet.middleground.contract.model.ArchiveManagementModel;
import com.ynet.middleground.contract.service.ArchiveManagementService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author daigaole
 * @modifyBy liulx
 */
@Api(value = "档案管理服务接口", tags = {"档案管理服务接口"})
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Path("archive")
@Service(timeout = 6000, version = "0.0.1")
public class ArchiveManagementServiceImpl implements ArchiveManagementService, GenericService {

    @Autowired
    ArchiveManagementModel archiveManagementModel;

    @POST
    @Path("new")
    @ApiOperation(value = "新建档案", notes = "新建档案业务场景描述，上传档案时使用此接口。")
    @ApiImplicitParam(name = "requestObj", dataType = "NewArchiveReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Integer> newArchive(NewArchiveReq requestObj) {
        ServiceResult<Integer> serviceResult = new ServiceResult<>();
        Integer result = null;
        try {
            result = archiveManagementModel.newArchive(requestObj);
            serviceResult.setResult(result);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setResult(result);
        return serviceResult;
    }

    @POST
    @Path("edit")
    @ApiOperation(value = "修改档案", notes = "修改档案业务场景，修改档案基础信息时使用此方法。")
    @ApiImplicitParam(name = "requestObj", dataType = "NewArchiveReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Boolean> editArchive(EditArchiveReq requestObj) {
        ServiceResult<Boolean> serviceResult = new ServiceResult<>();
        Boolean result = false;
        try {
            result = archiveManagementModel.editArchive(requestObj);
            serviceResult.setResult(result);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setResult(result);
        return serviceResult;
    }

    @POST
    @Path("query")
    @ApiOperation(value = "查看档案", notes = "查看档案")
    @ApiImplicitParam(name = "requestObj", dataType = "QueryArchiveReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<ArchiveDto>> queryArchive(QueryArchiveReq requestObj) {
        ServiceResult<List<ArchiveDto>> serviceResult = new ServiceResult<>();
        List<ArchiveDto> archiveDtos = null;
        try {
            archiveDtos = archiveManagementModel.queryArchive(requestObj);
            serviceResult.setResult(archiveDtos);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setPagerInfo(requestObj.getPagerInfo());
        serviceResult.setResult(archiveDtos);
        return serviceResult;
    }

    @POST
    @Path("delete")
    @ApiOperation(value = "删除档案", notes = "删除档案")
    @ApiImplicitParam(name = "requestObj", dataType = "DeleteArchiveReq", paramType = "body", required = true)
    @Override
    public ServiceResult<String> deleteArchive(DeleteArchiveReq requestObj) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        String result = null;
        try {
            result = archiveManagementModel.deleteArchive(requestObj);
            serviceResult.setResult(result);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setResult(result);
        return serviceResult;
    }

}
